/*
 * @Author: szx
 * @Date: 2022-05-16 13:57:11
 * @LastEditTime: 2022-05-16 14:02:04
 * @Description:
 * @FilePath: \leetcode\面试题\03\05\05.js
 */
var SortedStack = function () {
    this.stack = [];
};

/**
 * @param {number} val
 * @return {void}
 */
SortedStack.prototype.push = function (val) {
    if (this.isEmpty() || val < this.peek()) {
        this.stack.push(val);
    } else {
        const min = this.stack.pop();
        this.push(val);
        this.stack.push(min);
    }
};

/**
 * @return {void}
 */
SortedStack.prototype.pop = function () {
    return this.stack.pop();
};

/**
 * @return {number}
 */
SortedStack.prototype.peek = function () {
    return this.stack[this.stack.length - 1] ?? -1; //使用空值合并运算符，如果前者为空则取后者值
};

/**
 * @return {boolean}
 */
SortedStack.prototype.isEmpty = function () {
    return !this.stack.length;
};
